package cn.sail.shop3.api.controller;

import cn.sail.shop3.api.vo.BaseRespVO;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BaseServlet extends HttpServlet {
    /**
     * 业务处理成功返回
     *
     * @param resp response 对象
     * @param data 返回业务数据对象
     * @throws IOException
     */
    protected void successResp(HttpServletResponse resp, Object data) throws IOException {
        //构建标准返回内容
        BaseRespVO result = new BaseRespVO();
        result.setStatus(BaseRespVO.StatusType.SUCCESS.getValue());
        result.setData(data);
        //转换为json
        String json = JSON.toJSONString(result);
        //返回结果设置编码
        resp.setContentType("text/html;charset=UTF-8");
        //json写入到返回数据区域
        resp.getWriter().write(json);
    }
    //业务处理失败返回
    protected void errorResp(HttpServletResponse resp, String errorMsg) throws IOException {
        BaseRespVO result = new BaseRespVO();
        result.setStatus(BaseRespVO.StatusType.FAILED.getValue());
        result.setMsg(errorMsg);
        //转换为json
        String json = JSON.toJSONString(result);
        //返回结果设置编码
        resp.setContentType("text/html;charset=UTF-8");
        //json写入到返回数据区域
        resp.getWriter().write(json);
    }


    void test(){

    }

}
